<template>
    <div>
        <el-dialog :title="isAdd ? '添加管理员' : '编辑管理员'" :visible.sync="isShow" :before-close="cancel" center>
            <el-form :model="guanliForm" :rules="rules" ref="userinfo">
                <el-form-item label="所属角色" :label-width="formLabelWidth" prop="roleid">
                    <el-select v-model="guanliForm.roleid" placeholder="请选择">
                        <el-option v-for="item in rolelist" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="用户名称" :label-width="formLabelWidth" prop="username">
                    <el-input v-model="guanliForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="guanliForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="guanliForm.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button @click="confirm" type="primary">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {userAdd,userEdit,getUserInfo } from '../../request/api'
import {mapGetters,mapActions } from 'vuex'
export default {
    props:['isShow','isAdd'],
    data() {
        return {
            formLabelWidth: '120px',
            guanliForm: {
                roleid: '',
                username: '',
                password: '',
                status:1
            },
            rules: {
                username: [
                    { required: true, message: "请输入用户名称", trigger: "blur" },
                    { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
                ],
                roleid: [{ required: true, message: "请选择", trigger: "change" }],
            }
        }
    },
    mounted() { 
        if (this.rolelist.length == 0) {
            this.getRoleAction()
        }
    },
    computed: {
        ...mapGetters({
            rolelist:'role/getRole'
        })
    },
    methods: {
        ...mapActions({
            getRoleAction: 'role/getRoleAction',
            getUserList: 'user/getUserAction',
            getCount:'user/getCountAction'
        }),
        reset(){
            this.guanliForm = {
                roleid: '',
                username: '',
                password: '',
                status:1
            }
            this.$refs.userinfo.resetFields();
        },
        cancel() {
            this.reset()
            this.$emit('cancel',false)
        },
        //封装数据回显函数
        lookup(uid) {
            //调用获取一条数据接口
            getUserInfo({ uid }).then(res => {
                console.log(res);
                this.guanliForm = res.list
                this.guanliForm.uid = uid
                this.guanliForm.password = ''
            })
        },
        confirm() {
            this.$refs.userinfo.validate((val) => {
                if (val) {
                    //判断是否为添加还是编辑
                    if (this.isAdd) {
                        //调用添加接口
                        userAdd(this.guanliForm).then(res => {
                            if (res.code == 200) {
                                //添加成功
                                this.$message.success(res.msg)
                                // 重新获取列表
                                this.getUserList()
                                // 重新获取总条数
                                this.getCount()
                                //关闭弹框
                                this.cancel()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    } else {
                        //调用编辑接口
                        userEdit(this.guanliForm).then(res => {
                            if (res.code == 200) {
                                //添加成功
                                this.$message.success(res.msg)
                                // 重新获取列表
                                this.getUserList()
                                // 重新获取总条数
                                this.getCount()
                                //关闭弹框
                                this.cancel()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                }
            })
        }
    }
}
</script>

<style scoped>

</style>